package com.tonann.app.controller;

import cn.hutool.json.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.tonann.app.domain.AppProject;
import com.tonann.app.service.IAppProjectService;
import com.tonann.common.annotation.Log;
import com.tonann.common.core.controller.BaseController;
import com.tonann.common.core.domain.AjaxResult;
import com.tonann.common.core.domain.entity.SysDept;
import com.tonann.common.core.page.TableDataInfo;
import com.tonann.common.enums.BusinessType;
import com.tonann.common.utils.poi.ExcelUtil;
import com.tonann.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目管理Controller
 *
 * @author tonann
 * @date 2023-03-19
 */
@RestController
@RequestMapping("/app/project")
public class AppProjectController extends BaseController {
    @Autowired
    private IAppProjectService appProjectService;
    @Resource
    private ISysDeptService deptService;

    /**
     * 查询项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('app:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppProject appProject) {
        startPage();
        List<AppProject> list = appProjectService.selectAppProjectList(appProject);
        TableDataInfo tableDataInfo = getDataTable(list);
        List<AppProject> projectList =  (List<AppProject>)tableDataInfo.getRows();
        List<AppProject> voList = new ArrayList<>();
        for (AppProject project : projectList){
            if (project.getBusinessMan() != null){
                String businessMan = project.getBusinessMan();
                List<Long> longList = stringToLong(businessMan);
                project.setBusinessManIds(longList);
            }
            if (project.getMaintenanceMan() != null){
                String maintenanceMan = project.getMaintenanceMan();
                List<Long> longList = stringToLong(maintenanceMan);
                project.setMaintenanceManIds(longList);
            }
            if (project.getProjectFirstPartyDeptId() != null){
                SysDept firstPartyDept = deptService.selectDeptById(project.getProjectFirstPartyDeptId());
                project.setProjectFirstPartyDept(firstPartyDept);
            }
            if (project.getProjectGeneralContractorDeptId() != null){
                SysDept generalContractorDept = deptService.selectDeptById(project.getProjectGeneralContractorDeptId());
                project.setProjectGeneralContractorDept(generalContractorDept);
            }
            if (project.getProjectSupplierDeptId() != null){
                SysDept supplierDep = deptService.selectDeptById(project.getProjectSupplierDeptId());
                project.setProjectSupplierDept(supplierDep);
            }
            voList.add(project);
        }
        tableDataInfo.setRows(voList);
        return tableDataInfo;
    }


    /**
     * 查询项目管理列表
     */
    @GetMapping("/list/all")
    public AjaxResult listAll() {
        AppProject appProject = new AppProject();
        List<AppProject> list = appProjectService.selectAppProjectList(appProject);
        return AjaxResult.success(list);
    }

    /**
     * 导出项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('app:project:export')")
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppProject appProject) {
        List<AppProject> list = appProjectService.selectAppProjectList(appProject);
        ExcelUtil<AppProject> util = new ExcelUtil<AppProject>(AppProject.class);
        util.exportExcel(response, list, "项目管理数据");
    }

    /**
     * 获取项目管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:project:query')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId) {
        AppProject project =  appProjectService.selectAppProjectByProjectId(projectId);
        if (project.getBusinessMan() != null){
            String businessMan = project.getBusinessMan();
            List<Long> longList = stringToLong(businessMan);
            project.setBusinessManIds(longList);
        }
        if (project.getMaintenanceMan() != null){
            String maintenanceMan = project.getMaintenanceMan();
            List<Long> longList = stringToLong(maintenanceMan);
            project.setMaintenanceManIds(longList);
        }
        return AjaxResult.success(project);
    }
    /**
     * 新增项目管理
     */
    @PreAuthorize("@ss.hasPermi('app:project:add')")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppProject appProject) throws Exception {
        return toAjax(appProjectService.insertAppProject(appProject));
    }

    /**
     * 修改项目管理
     */
    @PreAuthorize("@ss.hasPermi('app:project:edit')")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppProject appProject) throws Exception {
        return toAjax(appProjectService.updateAppProject(appProject));
    }

    /**
     * 删除项目管理
     */
    @PreAuthorize("@ss.hasPermi('app:project:remove')")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds) {
        return toAjax(appProjectService.deleteAppProjectByProjectIds(projectIds));
    }

    private List<Long> stringToLong(String str){
        List<Long> list = new ArrayList<>();
        String[] strings = str.split(",");
        for(int i =0 ;i < strings.length; i++){
            String s = strings[i];
            if (s != null && !s.trim().isEmpty()) {
                Long l = Long.parseLong(strings[i]);
                list.add(l);
            }
        }
        return list;
    }
}
